// ! 渲染
function getTagList() {
    axios({
        method: 'GET',
        url: 'http://124.223.14.236:8060/admin/ydd_tag/all',
        headers: { 'x-token': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaW5mbyI6eyJpZCI6IjEiLCJsb2dpbl9uYW1lIjoiYWRtaW4iLCJwYXNzd29yZCI6Ijk2ZTc5MjE4OTY1ZWI3MmM5MmE1NDlkZDVhMzMwMTEyIiwibmFtZSI6IlRoaW5rSlPkuIDlk6UiLCJlbWFpbCI6Imhld3UxMTIyQDE2My5jb20iLCJwaG9uZSI6IjE3MzQyMDY1OTA5IiwibG9naW5faXAiOiI6OjEiLCJsb2dpbl9kYXRlIjoiMjAxOC0wMS0wOCAxNjoyMjo1MSIsImNyZWF0ZV9kYXRlIjoiMjAxNy0xMi0xNCAxMTo1Mjo0NiIsInVwZGF0ZV9kYXRlIjoiMjAyMi0wOC0wNiAyMToxODozNiIsImRlbF9mbGFnIjoiMCIsInJvbGVfaWQiOlsiMWM1NGUwMDNjMWZjNGRjZDliMDg3ZWY4ZDQ4YWJhYzMiXSwic3RhdHVzIjoxLCJkZWZhdWx0X2RhdGEiOiIxIiwicm9sZW5hbWUiOlsi5p-l55yL5bCx5a6M5LqLIl19LCJpYXQiOjE2NjAwMjczMzR9.4--3VxfT4P245AwDyw0uQyx7bUpBNRlDL9pdoWANw-U' }
    }).then(res => {
       let new_arr = res.data.data.map(function(item,index){
             return `
             
            <tr>
             <td>${index + 1}</td>
             <td>${item.id}</td>
             <td class="tagname">${item.tagname}</td>
             <td style="text-align: center; "><a style="display: inline-block; border-radius: 50%;
             width: 32px; height: 32px; background: #66b1ff; color: #fff; text-align: center;line-height: 32px;margin-right: 6px;"><i
                 class="layui-icon 
             layui-icon-edit"></i></a>
             <a class="aaa" data-id="${item.id}" style="display: inline-block; border-radius: 50%;
             width: 32px; height: 32px; background: #f78989; color: #fff; text-align: center;line-height: 32px;"><i
                 class="aaa layui-icon layui-icon-delete"></i></a></td>
             </tr> 
             
            `
       })
        $('#tb').html(new_arr.join(''))
        
    }).catch(err => {
       
   })
  }

getTagList()
let input = document.querySelector('#ipt')
input.addEventListener('click', function () {
    input.style.borderColor = '#409eff'
})

// ! 搜索
let search = document.querySelector('#search')
search.addEventListener('click', function () {
    let tip = document.querySelector('#ipt').value
    axios({
        method: 'GET',
        url: `http://124.223.14.236:8060/admin/ydd_tag/list?tagname=${tip}`,
        headers: { 'x-token': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaW5mbyI6eyJpZCI6IjEiLCJsb2dpbl9uYW1lIjoiYWRtaW4iLCJwYXNzd29yZCI6Ijk2ZTc5MjE4OTY1ZWI3MmM5MmE1NDlkZDVhMzMwMTEyIiwibmFtZSI6IlRoaW5rSlPkuIDlk6UiLCJlbWFpbCI6Imhld3UxMTIyQDE2My5jb20iLCJwaG9uZSI6IjE3MzQyMDY1OTA5IiwibG9naW5faXAiOiI6OjEiLCJsb2dpbl9kYXRlIjoiMjAxOC0wMS0wOCAxNjoyMjo1MSIsImNyZWF0ZV9kYXRlIjoiMjAxNy0xMi0xNCAxMTo1Mjo0NiIsInVwZGF0ZV9kYXRlIjoiMjAyMi0wOC0wNiAyMToxODozNiIsImRlbF9mbGFnIjoiMCIsInJvbGVfaWQiOlsiMWM1NGUwMDNjMWZjNGRjZDliMDg3ZWY4ZDQ4YWJhYzMiXSwic3RhdHVzIjoxLCJkZWZhdWx0X2RhdGEiOiIxIiwicm9sZW5hbWUiOlsi5p-l55yL5bCx5a6M5LqLIl19LCJpYXQiOjE2NjAwMjczMzR9.4--3VxfT4P245AwDyw0uQyx7bUpBNRlDL9pdoWANw-U' }
    }).then(res => {
        console.log(res);
       let new_arr = res.data.data.data.map(function(item,index){
             return `
             
            <tr>
             <td>${index + 1}</td>
             <td>${item.id}</td>
             <td class="tagname">${item.tagname}</td>
             <td style="text-align: center; "><a style="display: inline-block; border-radius: 50%;
             width: 32px; height: 32px; background: #66b1ff; color: #fff; text-align: center;line-height: 32px;margin-right: 6px;"><i
                 class="layui-icon 
             layui-icon-edit"></i></a>
             <a style="display: inline-block; border-radius: 50%;
             width: 32px; height: 32px; background: #f78989; color: #fff; text-align: center;line-height: 32px;"><i class="layui-icon layui-icon-delete"></i></a></td>
             </tr> 
             
            `
       })
        $('#tb').html(new_arr)
        
    }).catch(err => {
       console.log(err);
   })
    console.log(111)

})
// ! 重置
// let del = document.querySelector('.delete')
// let tb = document.querySelector('#tb')
let reset = document.querySelector('.btn2')
reset.addEventListener('click', function () {
    getTagList()
    $('#ipt').val('')
})

// ! 删除
let alerts = document.querySelector(".alert")
$('#tb').on('click', '.aaa', function (e) {
    console.log(e.target.className)
    if (e.target.className !== 'aaa' ) {
    console.log(111)
        return 
    }
    axios({
        method: 'GET',
        url: 'http://124.223.14.236:8060/admin/ydd_tag/del',
        params:{
            id:e.target.getAttribute('data-id')
        },
        
        headers: { 'x-token': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaW5mbyI6eyJpZCI6IjEiLCJsb2dpbl9uYW1lIjoiYWRtaW4iLCJwYXNzd29yZCI6Ijk2ZTc5MjE4OTY1ZWI3MmM5MmE1NDlkZDVhMzMwMTEyIiwibmFtZSI6IlRoaW5rSlPkuIDlk6UiLCJlbWFpbCI6Imhld3UxMTIyQDE2My5jb20iLCJwaG9uZSI6IjE3MzQyMDY1OTA5IiwibG9naW5faXAiOiI6OjEiLCJsb2dpbl9kYXRlIjoiMjAxOC0wMS0wOCAxNjoyMjo1MSIsImNyZWF0ZV9kYXRlIjoiMjAxNy0xMi0xNCAxMTo1Mjo0NiIsInVwZGF0ZV9kYXRlIjoiMjAyMi0wOC0wNiAyMToxODozNiIsImRlbF9mbGFnIjoiMCIsInJvbGVfaWQiOlsiMWM1NGUwMDNjMWZjNGRjZDliMDg3ZWY4ZDQ4YWJhYzMiXSwic3RhdHVzIjoxLCJkZWZhdWx0X2RhdGEiOiIxIiwicm9sZW5hbWUiOlsi5p-l55yL5bCx5a6M5LqLIl19LCJpYXQiOjE2NjAwMjczMzR9.4--3VxfT4P245AwDyw0uQyx7bUpBNRlDL9pdoWANw-U' }
    
        
    }).then(res => {
        // layer.open({
        //     title: false
        //     ,content: '确定删除吗,是否继续?',
        //     btn: ['取消', '确定'],
        //     area:'450px'
        //   }); 
        if (res.data.errno === 1000) {
        alerts.innerHTML = `<i class="layui-icon layui-icon-close;">&#x1006;</i>系统默认数据,不允许删除, 错误码1000`
            $('.alert').show()
            setTimeout(function () {
                $('.alert').hide()
    },3000)
        } else {
        e.target.parentNode.parentNode.style.display = 'none'
        }            
    console.log(res)
        
    }).catch(err => {
        console.log(err)
    })
})

// ! 新增
let add = document.querySelector('.new')
add.addEventListener('click', function () {
    layer.open({
        title: '添加标签'
        ,content: `标签名称 <input id="addtag" type="text" name="title" required lay-verify="required" placeholder="请输入标签名称" autocomplete="off" class="layui-input">`,
        area: '500px',
        offset: '150px',
        btn: ['取消', '确定']
      });  
    document.querySelector('body').addEventListener('click', function (e) {
        if (e.target.innerHTML == '确定') {
            console.log(1111)
            let adds = document.querySelector('#addtag').value
            axios({
                method: 'POST',
                url: 'http://124.223.14.236:8060/admin/ydd_tag/add',
                headers: { 'x-token': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaW5mbyI6eyJpZCI6IjEiLCJsb2dpbl9uYW1lIjoiYWRtaW4iLCJwYXNzd29yZCI6Ijk2ZTc5MjE4OTY1ZWI3MmM5MmE1NDlkZDVhMzMwMTEyIiwibmFtZSI6IlRoaW5rSlPkuIDlk6UiLCJlbWFpbCI6Imhld3UxMTIyQDE2My5jb20iLCJwaG9uZSI6IjE3MzQyMDY1OTA5IiwibG9naW5faXAiOiI6OjEiLCJsb2dpbl9kYXRlIjoiMjAxOC0wMS0wOCAxNjoyMjo1MSIsImNyZWF0ZV9kYXRlIjoiMjAxNy0xMi0xNCAxMTo1Mjo0NiIsInVwZGF0ZV9kYXRlIjoiMjAyMi0wOC0wNiAyMToxODozNiIsImRlbF9mbGFnIjoiMCIsInJvbGVfaWQiOlsiMWM1NGUwMDNjMWZjNGRjZDliMDg3ZWY4ZDQ4YWJhYzMiXSwic3RhdHVzIjoxLCJkZWZhdWx0X2RhdGEiOiIxIiwicm9sZW5hbWUiOlsi5p-l55yL5bCx5a6M5LqLIl19LCJpYXQiOjE2NjAwMjczMzR9.4--3VxfT4P245AwDyw0uQyx7bUpBNRlDL9pdoWANw-U' },
                data:{
                   tagname:adds  
                
            }
            }).then(res => {
               let new_arr = res.data.data.map(function(item,index){
                     return `
                     
                    <tr>
                     <td>${index + 1}</td>
                     <td>${item.id}</td>
                     <td class="tagname">${item.tagname}</td>
                     <td style="text-align: center; "><a style="display: inline-block; border-radius: 50%;
                     width: 32px; height: 32px; background: #66b1ff; color: #fff; text-align: center;line-height: 32px;margin-right: 6px;"><i
                         class="layui-icon 
                     layui-icon-edit"></i></a>
                     <a class="aaa" data-id="${item.id}" style="display: inline-block; border-radius: 50%;
                     width: 32px; height: 32px; background: #f78989; color: #fff; text-align: center;line-height: 32px;"><i
                         class="aaa layui-icon 
                     layui-icon-delete"></i></a></td>
                     </tr> 
                     
                    `
               })
                $('#tb').html(new_arr.join(''))
                
            }).catch(err => {
            //    alert('网络不好')
           })
          }
      })
})
getTagList()


